import { _decorator, AnimationClip, AssetManager, assetManager, AudioClip, error, Game, JsonAsset, Node, Prefab, resources, Sprite, SpriteFrame } from 'cc';
const { ccclass, property } = _decorator;

export class Loader {
    static dynamic: AssetManager.Bundle = null;
    static dynamicBg: AssetManager.Bundle = null;
    static dynamicPrefab: AssetManager.Bundle = null;
    static LoadJson(fileName: string, callBack: (js: JsonAsset) => void) {
        let strJsonPath: string = "Json/"
        this.dynamicPrefab.load<JsonAsset>(strJsonPath + fileName, JsonAsset, (error: Error, json: JsonAsset) => {
            if (!error) {
                callBack(json);
            }
        });
    }

    static LoadPrefab(fileName: string, callBack: (prefab: Prefab) => void) {
        let strPrefab: string = "prefab/"
        let res = resources.get<Prefab>(strPrefab + fileName, Prefab);
        if (res) {
            callBack(res);
            return;
        }
        resources.load<Prefab>(strPrefab + fileName, Prefab, (error: Error, json: Prefab) => {
            if (!error) {
                callBack(json);
                return;
            }
            console.error(error.name + " " + error.message);
        });
    };

    static LoadUiPrefab(fileName: string, callBack: (prefab: Prefab) => void) {
        let res = this.dynamicPrefab.get<Prefab>(fileName);
        if (res) {
            callBack(res);
            return;
        }
        this.dynamicPrefab.load<Prefab>(fileName, Prefab, (error: Error, json: Prefab) => {
            if (!error) {
                callBack(json);
                return;
            }
            console.error(error.name + " " + error.message);
        });
    };

    static ReleaseUiPrefab(fileName: string) {
        this.dynamicPrefab.release(fileName);
    }

    static PreLoadUIPrefab(fileName: string) {
        let strPrefab: string = "prefab/"
        resources.preload(strPrefab + fileName);
    };

    static GetUiPrefab(fileName: string): Prefab {
        return this.dynamicPrefab.get<Prefab>(fileName, Prefab);
    }

    static GetPrefab(fileName: string): Prefab {
        let strPrefab: string = "prefab/"
        return resources.get<Prefab>(strPrefab + fileName, Prefab);
    }

    static GetImage(fileName: string): SpriteFrame {
        let res = Loader.GetBundleByGame();
        return res.get<SpriteFrame>(fileName + "/spriteFrame" + fileName, SpriteFrame);
    }

    static LoadImage(fileName: string, callBack: (sp: SpriteFrame) => void) {
        let res = Loader.GetBundleByGame();
        res.load(fileName + "/spriteFrame", SpriteFrame, null, (err: Error, spf: SpriteFrame) => {
            if (!err) {
                if (callBack) callBack(spf);
                return;
            }
            console.error(err.name + " " + err.message);
        })
    }

    static LoadBulletImage(fileName: string, callBack: (sp: SpriteFrame) => void) {
        this.dynamicBg.load(fileName + "/spriteFrame", SpriteFrame, null, (err: Error, spf: SpriteFrame) => {
            if (!err) {
                if (callBack) callBack(spf);
                return;
            }
            console.error(err.name + " " + err.message);
        })
    }

    static LoadBg(fileName: string, callBack: (sp: SpriteFrame) => void) {
        this.dynamicBg.load(fileName + "/spriteFrame", SpriteFrame, null, (err: Error, spf: SpriteFrame) => {
            if (!err) {
                if (callBack) callBack(spf);
                return;
            }
            console.error(err.name + " " + err.message);
        })
    }

    static LoadGunImage(fileName: string, callBack: (sp: SpriteFrame) => void) {
        this.dynamicBg.load(fileName + "/spriteFrame", SpriteFrame, null, (err: Error, spf: SpriteFrame) => {
            if (!err) {
                if (callBack) callBack(spf);
                return;
            }
            console.error(err.name + " " + err.message);
        })
    }

    static ReleaseImage(fileName: string, callBack?: (sp: SpriteFrame) => void) {
        resources.release(fileName + "/spriteFrame");
    }

    static ReleasePrefab(fileName: string) {
        let strPrefab: string = "prefab/";
        resources.release(strPrefab + fileName, Prefab);
    }

    static LoadSound(fileName: string, callBack: (audio: AudioClip) => void) {
        let strAudio: string = "audio/";
        if (this.dynamicBg.get(strAudio + fileName, AudioClip)) {
            if (callBack) callBack(this.dynamicBg.get(strAudio + fileName, AudioClip));
            return;
        }
        this.dynamicBg.load(strAudio + fileName, AudioClip, (err: Error, data: AudioClip) => {
            if (!err) {
                if (callBack) callBack(data);
                return;
            }
            console.error("Load Sound " + fileName + "error" + err.name + ":" + err.message);
        })
    }

    static LoadSoundDir(callBack?: () => void) {
        let strAudioPath: string = "audio";
        this.dynamicBg.loadDir(strAudioPath, AudioClip, (finished: number, total: number, item: any) => {
            if (finished == total) {
                if (callBack) callBack();
            }
        }, null);
    }

    static LoadMonsterImageDir(path: string, callBack: (arr: SpriteFrame[]) => void) {
        let imgPath: string = "monster/" + path;
        this.dynamic.loadDir<SpriteFrame>(imgPath, SpriteFrame, (err: Error | null, data) => {
            data.sort((a, b) => {
                return Number.parseInt(a.name) - Number.parseInt(b.name);
            })
            if (callBack) callBack(data);
        });
    }

    static LoadSkillImageDir(path: string, callBack: (arr: SpriteFrame[]) => void) {
        let imgPath: string = "bullet/" + path;
        this.dynamicBg.loadDir<SpriteFrame>(imgPath, SpriteFrame, (err: Error | null, data) => {
            data.sort((a, b) => {
                return Number.parseInt(a.name) - Number.parseInt(b.name);
            })
            if (callBack) callBack(data);
        });
    }

    static PreLoadEffectImageDir(path: string, callBack?: (arr) => void) {
        let imgPath: string = "effect/" + path;
        this.dynamic.preloadDir(imgPath, SpriteFrame, (err: Error | null, data) => {
            if (callBack) callBack(data);
        });
    }

    static LoadEffectImageDir(path: string, callBack: (arr: SpriteFrame[]) => void) {
        let imgPath: string = "effect/" + path;
        this.dynamic.loadDir<SpriteFrame>(imgPath, SpriteFrame, (err: Error | null, data) => {
            data.sort((a, b) => {
                return Number.parseInt(a.name) - Number.parseInt(b.name);
            })
            if (callBack) callBack(data);
        });
    }

    static LoadMuzzleImageDir(path: string, callBack: (arr: SpriteFrame[]) => void) {
        let imgPath: string = "muzzleFlash/" + path;
        this.dynamicBg.loadDir<SpriteFrame>(imgPath, SpriteFrame, (err: Error | null, data) => {
            data.sort((a, b) => {
                return Number.parseInt(a.name) - Number.parseInt(b.name);
            })
            if (callBack) callBack(data);
        });
    }

    static DynamicBgLoadDir(path: string, callBack: (arr: SpriteFrame[]) => void) {
        this.dynamicBg.loadDir<SpriteFrame>(path, SpriteFrame, (err: Error | null, data) => {
            data.sort((a, b) => {
                return Number.parseInt(a.name) - Number.parseInt(b.name);
            })
            if (callBack) callBack(data);
        });
    }

    static GetBundleByGame(): AssetManager.Bundle {
        return resources;
    }

    static GetTurnTableRewardImage(fileName: string, callBack) {
        if (this.dynamicBg.get(fileName, SpriteFrame)) {
            if (callBack) callBack(this.dynamicBg.get(fileName, SpriteFrame));
            return;
        }
        this.dynamicBg.load(fileName + "/spriteFrame", SpriteFrame, null, (err: Error, spf: SpriteFrame) => {
            if (!err) {
                if (callBack) callBack(spf);
                return;
            }
            console.error(err.name + " " + err.message);
        })
    }

    static InitBundle(callbcak: () => void) {
        if (callbcak) callbcak();
        // switch (GameData.mCurGame) {
        //     case Games.hotWords:
        //         if (callbcak) callbcak();
        //         break;
        //     case Games.idiomRelay:
        //         if (callbcak) callbcak();
        //         break;
        //     case Games.textElimination:
        //         if (callbcak) callbcak();
        //         break;
        //     case Games.findFault:
        //         Loader.InitFindFault(callbcak);
        //         break;
        //     case Games.defult:
        //         if (callbcak) callbcak();
        //         break;

        // }
    }
}


